Skip to content

docs: הוספת מדריך סנכרון בזמן אמת ואימות בטלגרם - #3128

Open
amirbiron wants to merge 1 commit into
mainfrom
claude/blog-sync-telegram-auth-6JlpB
Open

docs: הוספת מדריך סנכרון בזמן אמת ואימות בטלגרם#3128
amirbiron wants to merge 1 commit into
mainfrom
claude/blog-sync-telegram-auth-6JlpB

Conversation

@amirbiron

Copy link
Copy Markdown
Owner

✨ תיאור קצר

הוספת מדריך תיעוד מקיף בעברית המסביר כיצד לממש סנכרון בזמן אמת בין Telegram Bot ל-Web App, וכיצד לאמת משתמשים בצורה מאובטחת באמצעות Telegram Login Widget וטוקנים חד-פעמיים.

📦 שינויים עיקריים

  • תיעוד (docs/)

פירוט:

  • הוספת קובץ docs/blog/sync-and-auth-telegram-webapp.md (301 שורות)
  • המדריך מכסה שני נושאים עיקריים:
    1. סנכרון בזמן אמת – Single Source of Truth עם DB משותף, Repository אחיד, ו-Cache Invalidation
    2. אימות משתמשים – שתי שיטות: Telegram Login Widget (HMAC-SHA256) וטוקנים חד-פעמיים מהבוט
  • כולל דוגמאות קוד מעשיות בPython (Flask, MongoDB)
  • דיאגרמות ASCII להמחשת הארכיטקטורה
  • טיפים למימוש ותרשים סיכום

🧪 בדיקות

  • סוג בדיקה: תיעוד בלבד – אין קוד ייצור או בדיקות יחידה
  • בדיקה ידנית: קובץ Markdown תקין, קריא, וללא שגיאות תחביר
  • אין השפעה על CI/CD: שינוי תיעוד בלבד

📝 סוג שינוי

  • docs: שינוי תיעוד בלבד

✅ צ'קליסט

  • קובץ Markdown תקין וקריא
  • אין סודות/מפתחות בקוד (דוגמאות בלבד)
  • אין מחיקות מסוכנות
  • הודעת הקומיט תואמת Conventional Commits

🧩 השפעות/סיכונים

  • השפעה: אין – שינוי תיעוד בלבד, ללא השפעה על קוד ייצור או בדיקות

🔗 קישורים

  • מדריך מלא: docs/blog/sync-and-auth-telegram-webapp.md

https://claude.ai/code/session_019AtMGvdXX68pMCGrSiKFEr

פוסט שמסביר שני נושאים מרכזיים בארכיטקטורה:
1. סנכרון בין בוט טלגרם ל-Web App באמצעות DB משותף ושכבת Repository אחידה
2. אימות משתמשים ב-Web App דרך Telegram Login Widget ו-Token חד-פעמי

https://claude.ai/code/session_019AtMGvdXX68pMCGrSiKFEr
@github-actions

Copy link
Copy Markdown

🧯 Dangerous deletes guard report

Policy: see .cursorrules — dangerous deletions are blocked unless wrapped safely.

Summary:

  • Flagged findings (blocking): 0
    0
  • Excluded matches (not blocking): 15
  • Total matches (all files): 129

Flagged findings (file:line:snippet):
(none)

Excluded matches (by path pattern)
./webapp/static/js/md_preview.bundle.js.map:4:  "sourcesContent": ["// Markdown-it plugin to render GitHub-style task lists; see\n//\n// https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments\n// https://github.com/blog/1825-t … [truncated]
./README.md:842:find . -name "__pycache__" -exec rm -rf {} +
./node_modules/mermaid/dist/mermaid.min.js:1524:`,"getStyles"),c1e=RQe});var h1e={};dr(h1e,{diagram:()=>NQe});var NQe,f1e=N(()=>{"use strict";$ge();a1e();l1e();u1e();NQe={parser:Fge,db:n1e,renderer:o1e,styles:c1e}});var m1e,g1e=N(()=>{"use  … [truncated]
./node_modules/mermaid/dist/chunks/mermaid.esm.min/chunk-4HFYJGYH.mjs:1:var r={name:"mermaid",version:"11.12.0",description:"Markdown-ish syntax for generating flowcharts, mindmaps, sequence diagrams, class diagrams, gantt charts, git graph … [truncated]
./node_modules/mermaid/dist/chunks/mermaid.esm.min/chunk-4HFYJGYH.mjs.map:4:  "sourcesContent": ["{\n  \"name\": \"mermaid\",\n  \"version\": \"11.12.0\",\n  \"description\": \"Markdown-ish syntax for generating flowcharts, mindmaps, sequen … [truncated]
./node_modules/mermaid/dist/chunks/mermaid.esm/chunk-2M32CCKP.mjs.map:4:  "sourcesContent": ["{\n  \"name\": \"mermaid\",\n  \"version\": \"11.12.0\",\n  \"description\": \"Markdown-ish syntax for generating flowcharts, mindmaps, sequence d … [truncated]
./node_modules/mermaid/dist/chunks/mermaid.core/chunk-KS23V3DP.mjs.map:4:  "sourcesContent": ["{\n  \"name\": \"mermaid\",\n  \"version\": \"11.12.0\",\n  \"description\": \"Markdown-ish syntax for generating flowcharts, mindmaps, sequence  … [truncated]
./node_modules/mermaid/dist/mermaid.js.map:4:  "sourcesContent": ["/**\n* Default values for dimensions\n*/\nconst defaultIconDimensions = Object.freeze({\n\tleft: 0,\n\ttop: 0,\n\twidth: 16,\n\theight: 16\n});\n/**\n* Default values for tr … [truncated]
./node_modules/mermaid/dist/mermaid.min.js.map:4:  "sourcesContent": ["/**\n* Default values for dimensions\n*/\nconst defaultIconDimensions = Object.freeze({\n\tleft: 0,\n\ttop: 0,\n\twidth: 16,\n\theight: 16\n});\n/**\n* Default values fo … [truncated]
./node_modules/katex/package.json:153:    "build": "rimraf dist/ && mkdirp dist && cp README.md dist && rollup -c --failAfterWarnings && webpack && node update-sri.js package dist/README.md",
./node_modules/katex/src/fonts/Makefile:139:	rm -rf pfa ff otf ttf woff woff2
./docs/Makefile:24:	rm -rf $(BUILDDIR)
./docs/DOCUMENTATION_GUIDE.md:453:rm -rf _build
./Dockerfile:42:    rm -rf /var/lib/apt/lists/*
./Dockerfile:121:    rm -rf /var/lib/apt/lists/*

@github-actions

Copy link
Copy Markdown

⏱️ Performance report

(No performance test durations collected. Mark tests with @pytest.mark.performance.)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f89740029

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +191 to +192
token_data = f"{user_id}:{int(time.time())}:{secret}"
auth_token = hashlib.sha256(token_data.encode()).hexdigest()[:32]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Generate truly random login tokens

The token in build_login_payload is deterministic (user_id + current second + secret), so multiple login-link requests from the same user within the same second produce the exact same token. That breaks the “one-time” guarantee described in the doc because duplicate DB rows can exist for one token, allowing the same URL to be accepted repeatedly until all duplicates are consumed; use a cryptographically random nonce (for example secrets.token_urlsafe) and enforce uniqueness on the token field.

Useful? React with 👍 / 👎.

Comment on lines +217 to +220
token_doc = db.webapp_tokens.find_one({
"token": token,
"user_id": int(user_id)
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Consume one-time token with a single atomic DB operation

The flow first reads the token document and only later deletes it, which creates a race where two parallel requests with the same URL can both pass find_one before either deletion runs, resulting in multiple valid sessions from one supposedly single-use token. Document this as an atomic find_one_and_delete (with token, user, and expiry conditions) so only one request can successfully consume the token.

Useful? React with 👍 / 👎.

@github-actions

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully!

To view locally:

  1. Download the artifacts
  2. Extract the zip file
  3. Open index.html in your browser

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

).hexdigest()

# השוואה + בדיקת תוקף זמן (עד שעה)
if calculated_hash != check_hash:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HMAC hash comparison vulnerable to timing attacks

High Severity

The verify_telegram_auth code example uses calculated_hash != check_hash (direct string comparison) instead of hmac.compare_digest(calculated_hash, check_hash). Direct != comparison is vulnerable to timing attacks because it short-circuits on the first differing byte, allowing attackers to infer the correct hash incrementally. Since this guide explicitly teaches "secure authentication" and the hmac module is already imported, the constant-time hmac.compare_digest() function is the correct choice here.

Fix in Cursor Fix in Web

return "קישור פג תוקף", 401

# מחיקה אחרי שימוש — חד פעמי!
db.webapp_tokens.delete_one({"_id": token_doc["_id"]})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token auth has TOCTOU race allowing reuse

Medium Severity

The one-time token verification uses separate find_one and delete_one calls, creating a TOCTOU (time-of-check-time-of-use) race condition. Two concurrent requests with the same token can both pass the find_one check before either executes delete_one, defeating the "one-time use" guarantee the guide emphasizes. MongoDB's atomic find_one_and_delete is the correct approach for consuming one-time tokens.

Fix in Cursor Fix in Web

@sentry

sentry Bot commented Mar 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@amirbiron

Copy link
Copy Markdown
Owner Author

@claude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants